<?php

require('config.php');
$language_link=$_GET['id'];

if($language_link != null && $language_link > 0 ) {
	$district = modelDistrict::getDistrict($language_link);
	if(!$district)redirectFront(index);
}else{
	redirectFront(index);
}
/*if($district){
	if(!$_GET['preview']){
		if($district->id_menu_left_tree != null and $district->id_menu_left_tree > 0){
				if(!isset($_SESSION['id_user'])){
					$_SESSION['referer_connexion'] =  "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
					redirectFront('connexion');
				}
		}else redirectFront('connexion');
	}
}else redirectFront('index');*/

$html_name = $district->url_rewriting.'-'.$language_link.'.html';



if(isset($_GET['title_debug']) && trim($_GET['title_debug'])!=''){
	$_SESSION['title_debug'] = $_GET['title_debug'];
	$title_debug = $_GET['title_debug'];
}
else {
	unset($_SESSION['title_debug']);
}

$page = new rootLayoutPage();
$page->addStyleSheet('table_sorter.css');
$page->addJavaScript('jQuery.pagination.js');
$page->addStyleSheet('pagination.css');

$page->importJavaScript('http://maps.google.com/maps?file=api&amp;v=2&amp;key='.GOOGLE_MAP_KEY);
$page->importJavaScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0');
$page->importJavaScript('http://www.google.com/uds/solutions/localsearch/gmlocalsearch.js');
$page->importStyleSheet('http://www.google.com/uds/css/gsearch.css');
$page->importStyleSheet('http://www.google.com/uds/solutions/localsearch/gmlocalsearch.css');

$xml = tmxTranslate('layoutFooter.xml', REP_XLAT_LAYOUT);
$page->setPageSameLevel($district->id_menu_left_tree);
//-- Menu Page ----------------------------------------------//
$menu = new layoutMenuLeft();
	$menuPage = new layoutMenuPage();
	$menuPage->setTable('district');
	$menuPage->setIdShow($district->id_menu_left_tree);
	$menu->setIdShow($district->id_menu_left_tree);
	$menu->pushMenu($menuPage);

//--------Menu indicator--------------//
$menuIndictor = new layoutMenuIndicator();
	$menuIndictor->idTree($district->id_menu_left_tree);
	$menuIndictor->title($district->title);
	$page->setMenuIndicator($menuIndictor);

//************************//
$arr = array();
$getListUrlDistrict = modelDistrict::getListUrlDistrict($language_link);
foreach($getListUrlDistrict AS $key=>$Url){
	$arr[$Url->language_code]=$Url->url_rewriting.'-'.$Url->language_link.'.html';
}
$header = new layoutHeader();
$header->setArrayUrl($arr);
$header->setIdShow($district->id_menu_left_tree);
$page->setHeader($header);

$page->setMenuPage($menu);
$page->setBaliseTitle($district->meta_title);
$page->setDescription($district->meta_description);
$extension = filename_extension($_SERVER['REQUEST_URI']);
$page->setCanonical($html_name);

$tpl_file = 'district.tpl';
$Body = new flyLayout(REP_TPL . $tpl_file);
$Body->start();
	$Body->setVariable('text_status',$xml['text_status']);
	$Body->setVariable('title_rented_sold',$xml['title_rented_sold']);	
	$Body->setVariable('text_location',$xml['text_location']);
	$Body->setVariable('text_price_sell',$xml['text_price_sell']);
	$Body->setVariable('text_price_rental',$xml['text_price_rental']);
	$Body->setVariable('text_type',$xml['text_type']);
	
	$Body->setVariable('page_title',$district->title);
	$map = modelDistrict::getDistrict($language_link);
	if($map and $map->latitude > 0 and $map->longitude > 0 and $map->zome_level > 0){
    	$Body->setVariable('latitude_map', $map->latitude);
		$Body->setVariable('longitude_map', $map->longitude);
		$Body->setVariable('zome_level', $map->zome_level);
    }else{
    	$Body->setVariable('latitude_map', DEFAUFT_LATITUDE);
		$Body->setVariable('longitude_map', DEFAUFT_LONGITUDE);
		$Body->setVariable('zome_level', DEFAUFT_ZOME_LEVEL);
    }
	//****Pagination***********//
	if(isset($_GET["page"]) > 1) {
		$id_page = $_GET['page'];
	    $limit = (($id_page-1)*ITEM_PER_PAGE_REAL_ESTATE)." , ".ITEM_PER_PAGE_REAL_ESTATE;;
	}else{
		$id_page = '';
	    $limit = '0 , '.ITEM_PER_PAGE_REAL_ESTATE;
	}

	$format = modelUploadFormat::getObject(FORMAT_PHOTO_LIST);
	$format_zoom = modelUploadFormat::getObject(FORMAT_PREVIEW_PHOTO_LIST);
	 $listAllDistrict = modelDistrict::getListDistrictByid($language_link,$limit, $title_debug);
	if($listAllDistrict->size() > 0){
		foreach($listAllDistrict as $key => $location){
			$Body->resetList('location_photo');
			if($location->url_repertoire){
				if(file_exists($location->url_repertoire.$format->url_format.$location->nom_serveur) and isImageFileName($location->nom_serveur)){
					$fichier = modelUploadFile::getObject($location->id_fichier);
					list($width, $height, $type, $attr) = getimagesize($fichier->url_repertoire.$format->url_format.$fichier->nom_serveur);
					$url_image = $fichier->url_repertoire.$format->url_format.$fichier->nom_serveur;
					if(file_exists($location->url_repertoire.$format_zoom->url_format.$location->nom_serveur) and isImageFileName($location->nom_serveur)){
						$url_image_zoom = $fichier->url_repertoire.$format_zoom->url_format.$fichier->nom_serveur;
					}else{
						$url_image_zoom = REP_DESIGN.'noimage.jpg';
					}
					$Body->setVariable('src_photo',$url_image);
					$Body->setVariable('image_preview',$url_image_zoom);
					$Body->setVariable('width',$width.'px');
					$Body->setVariable('height',$height.'px');
					$Body->setVariable('url_photo',$location->url_rewriting.'-'.$location->language_link.'.html');
					$Body->parseList('location_photo');
					$Body->showBlock('block_place_photo');
				}else{
					$Body->hideBlock('block_place_photo');
				}
			}else{
				$Body->hideBlock('block_place_photo');
			}
			
			$comma = array();
			$i = 0;
			if($location->length>0){
				$comma[$i] = $xml['label_length'].' '.$location->length.$xml['label_meter'] ;$i=$i+1;
			}
			if($location->width>0){
				$comma[$i] = $xml['label_width'].' '.$location->width.$xml['label_meter'] ;$i=$i+1;
			}
			if($location->size_building>0){
				$comma[$i] = $xml['label_area'].' '.$location->size_building.$xml['label_meter_square'] ;$i=$i+1;
			}
			if($location->length_land>0){
				$comma[$i] = $xml['label_land_length'].' '.$location->length_land.$xml['label_meter'] ;$i=$i+1;
			}
			if($location->width_land>0){
				$comma[$i] = $xml['label_land_width'].' '.$location->width_land.$xml['label_meter']  ;$i=$i+1;
			}
			if($location->size_land>0){
				$comma[$i] = $xml['label_land_area'].' '.$location->size_land.$xml['label_meter_square'] ;$i=$i+1;
			}
			if($location->bedrooms>0){
				$comma[$i] = $xml['label_bedrooms'].' '.$location->bedrooms ;$i=$i+1;
			}
			if($location->bathrooms>0){
				$comma[$i] = $xml['label_bathrooms'].' '.$location->bathrooms ;$i=$i+1;
			}
			$Body->setVariable('id_addr',$key);
			$count = count($comma);
			$string = '' ;
			foreach($comma AS $key => $c){
				 $count==$key+1?$string.=$c:$string.=$c.', ';
			}
			$Body->setVariable('place_description',$string);

			$Body->setVariable('meta_title',$location->meta_title);
			$Body->setVariable('place_name',$location->name_place);
			$Body->setVariable('map_address',nl2br($location->address));
			

			$Body->setVariable('real_estate',$location->estate_tile);
			$Body->setVariable('type_meta_title',$location->type_meta_title);

			$Body->setVariable('price_sell',$location->price_sell<>0?number_format($location->price_sell,0,'.',' ').' '.CURRENCY:'');
			$Body->setVariable('price_rental',$location->price_rental<>0?number_format($location->price_rental,0,'.',' ').' '.CURRENCY:'');

			$Body->setVariable('url_category_map', $location->url_rewriting.'-'.$location->language_link.'.html');
			$Body->parseList('list_category_map');
		}
		$Body->showBlock('district');
	}

	/* include pagination**/
	$format_map = modelUploadFormat::getObject(FORMAT_PREVIEW_MAP);
	$listAllDistrict = modelDistrict::getListDistrictByid($language_link);
		foreach($listAllDistrict as $key => $locationAll){
		if($locationAll->url_repertoire){
			if(file_exists($locationAll->url_repertoire.$format_map->url_format.$locationAll->nom_serveur) and isImageFileName($locationAll->nom_serveur)){
				$fichier_map = modelUploadFile::getObject($locationAll->id_fichier);
				list($width, $height, $type, $attr) = getimagesize($fichier_map->url_repertoire.$format_map->url_format.$fichier_map->nom_serveur);
				$url_image_map = '/'.$fichier_map->url_repertoire.$format_map->url_format.$fichier_map->nom_serveur;
				$Body->setVariable('map_photo',$url_image_map);
				$Body->setVariable('map_width',$width.'px');
				$Body->setVariable('map_height',$height.'px');
				$Body->showBlock('map_image');
			}else{
				$Body->setVariable('map_photo','');
				$Body->hideBlock('map_image');
			}

		}else{
			$Body->setVariable('map_photo','');
			$Body->hideBlock('map_image');
		}

		if($locationAll->latitude and $locationAll->longitude){
			$Body->setVariable('latitude', $locationAll->latitude);
			$Body->setVariable('longitude', $locationAll->longitude);
			$Body->showBlock('map_canvas_location');
		}else{
			$Body->hideBlock('map_canvas_location');
		}

		$Body->setVariable('map_place_name',addslashes($locationAll->name_place));
		//$Body->setVariable('map_address',addslashes($locationAll->address));
		$Body->setVariable('addr',$key);
		$Body->setVariable('map_province',addslashes($locationAll->name_province));
		$Body->setVariable('map_meta_title',addslashes($locationAll->meta_title));
		$Body->setVariable('url_map', addslashes($locationAll->url_rewriting.'-'.$locationAll->language_link.'.html'));


		if($locationAll->name_place){
			$Body->showBlock('map_place_name');
		}else{
			$Body->hideBlock('map_place_name');
		}
		if($locationAll->address){
			$Body->showBlock('map_address');
		}else{
			$Body->hideBlock('map_address');
		}
		if($locationAll->name_province){
			$Body->showBlock('map_province');
		}else{
			$Body->hideBlock('map_province');
		}
		$Body->parseList('listGoogleMapJavascript');

	}

	$arrayTpl = array();
	$arrayPagination = array();
	$arrayTpl[1]='pagination_search_top';
	$arrayTpl[2]='pagination_search_bottom';
	for($i=1;$i<=2;$i++){
		if (file_exists(REP_TPL . $_SESSION['rep_language'] . $arrayTpl[$i].'.tpl')){
		$arrayPagination[$i] = new flyLayout(REP_TPL . $arrayTpl[$i].'.tpl');
		}
		else{$arrayPagination[$i] = new flyLayout(REP_TPL . $arrayTpl[$i].'.tpl');
		}
		$arrayPagination[$i]->start();
		$arrayPagination[$i]->setVariable('rep_ajax', REP_AJAX.'product/');
		$arrayPagination[$i]->setVariable('rep_item_lister', 'list_search_location_district.php?lang='.$_SESSION['language_code'].'&id='.$language_link);
		$i==1?$arrayPagination[$i]->setVariable('rep_item_counter', 'list-location-district-search-count-item.php?lang='.$_SESSION['language_code'].'&id='.$language_link):'';
		$i==1?$arrayPagination[$i]->setVariable('page',  $id_page):'';
		$i==1?$arrayPagination[$i]->setVariable('item_per_page', ITEM_PER_PAGE_REAL_ESTATE):'';
		$i==1?$arrayPagination[$i]->setVariable('num_display_entries', NUM_DISPLAY_ENTRIES):'';
		$i==1?$arrayPagination[$i]->setVariable('delay_search', DELAY_SEARCH):'';
		$i==1?$arrayPagination[$i]->setVariable('delay_fifty_mili_second', DELAY_FIFTY_MILI_SECOND):'';
		$i==1?$arrayPagination[$i]->setVariable('default_text_search', $_SESSION['title_debug']):'';
		$i==1?$arrayPagination[$i]->setVariable('num_items', $listAllDistrict->size()):'';

		$arrayPagination[$i]->showBlock('pagination_search');
		$arrayPagination[$i]->stop();
	}
    //$Body->includeFile('pagination_search_up', $arrayPagination[1]);
    //$Body->includeFile('pagination_search_down', $arrayPagination[2]);

//******Rented and sold****//
		 $listAllcategory = modelRealEstate::getListPropertiesDistrictRentedSoldAuto($language_link,MAX_RENTED_SOLD_LISTS);
		 if($listAllcategory->size()>0){
			foreach($listAllcategory as $key => $location){
				$Body->resetList('location_photo');
				if($location->url_repertoire){
					if(file_exists($location->url_repertoire.$format->url_format.$location->nom_serveur) and isImageFileName($location->nom_serveur)){
						$fichier = modelUploadFile::getObject($location->id_fichier);
						list($width, $height, $type, $attr) = getimagesize($fichier->url_repertoire.$format->url_format.$fichier->nom_serveur);
						$url_image = $fichier->url_repertoire.$format->url_format.$fichier->nom_serveur;
						if(file_exists($location->url_repertoire.$format_zoom->url_format.$location->nom_serveur) and isImageFileName($location->nom_serveur)){
							$url_image_zoom = $fichier->url_repertoire.$format_zoom->url_format.$fichier->nom_serveur;
						}else{
							$url_image_zoom = REP_DESIGN.'noimage.jpg';
						}
						$Body->setVariable('src_photo',$url_image);
						$Body->setVariable('image_preview',$url_image_zoom);
						$Body->setVariable('width',$width.'px');
						$Body->setVariable('height',$height.'px');
						$Body->setVariable('url_photo',$location->url_rewriting.'-'.$location->language_link.'.html');
						$Body->parseList('location_photo');
						$Body->showBlock('block_place_photo');
					}else{
						$Body->hideBlock('block_place_photo');
					}
				}else{
					$Body->hideBlock('block_place_photo');
				}

				$Body->setVariable('meta_title',$location->meta_title);
				$Body->setVariable('place_name',$location->name_place);

				$comma = array();
				$i = 0;
				if($location->length>0){
					$comma[$i] = $xml['label_length'].' '.$location->length.$xml['label_meter'] ;$i=$i+1;
				}
				if($location->width>0){
					$comma[$i] = $xml['label_width'].' '.$location->width.$xml['label_meter'] ;$i=$i+1;
				}
				if($location->size_building>0){
					$comma[$i] = $xml['label_area'].' '.$location->size_building.$xml['label_meter_square'] ;$i=$i+1;
				}
				if($location->length_land>0){
					$comma[$i] = $xml['label_land_length'].' '.$location->length_land.$xml['label_meter'] ;$i=$i+1;
				}
				if($location->width_land>0){
					$comma[$i] = $xml['label_land_width'].' '.$location->width_land.$xml['label_meter']  ;$i=$i+1;
				}
				if($location->size_land>0){
					$comma[$i] = $xml['label_land_area'].' '.$location->size_land.$xml['label_meter_square'] ;$i=$i+1;
				}
				if($location->bedrooms>0){
					$comma[$i] = $xml['label_bedrooms'].' '.$location->bedrooms ;$i=$i+1;
				}
				if($location->bathrooms>0){
					$comma[$i] = $xml['label_bathrooms'].' '.$location->bathrooms ;$i=$i+1;
				}

				$count = count($comma);
				$string = '' ;
				foreach($comma AS $key => $c){
					 $count==$key+1?$string.=$c:$string.=$c.', ';
				}
				$Body->setVariable('place_description',$string);
				
				if(($location->status == LOCATION_SELL) or ($location->status == LOCATION_RENTED)){
					if($location->status == LOCATION_SELL){
						$Body->setVariable('text_reanted_sold_list', 'logo_sold_list_'.$_SESSION['language_code']);
					}
					if($location->status == LOCATION_RENTED){
						$Body->setVariable('text_reanted_sold_list', 'logo_rent_list_'.$_SESSION['language_code']);
					}		
					$Body->showBlock('text_reanted_sold_list');
				}

				$Body->setVariable('status',$location->status_property);

				$Body->setVariable('real_estate',$location->title_map);
				$Body->setVariable('type_meta_title',$location->type_meta_title);

				$Body->setVariable('url_category_map', $location->url_rewriting.'-'.$location->language_link.'.html');
				$Body->parseList('list_rented_sold');
				$nothing = false;
			}
			$Body->showBlock('list_place');
		 }
$Body->stop();
$page->setBody($Body);
$page->display();

?>